/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  main.c - main control loop for SRV-1 robot
 *    Copyright (C) 2005-2007  Surveyor Corporation
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details (www.gnu.org/licenses)
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "srv.h"
#include "uart.h"
#include "malloc.h"
#include "robochallenge.h"

int main() {
    unsigned char ch;
    init_heap();
    init_io(); // Initialise LED, GPIO, serial flow.
    initRTC();
    init_uart0();
    init_colors();
    serial_out_version();
    clear_sdram(); // Clears from 0x00100000 to 0x02000000
    camera_setup(); // Sets up the camera to 320x256
    led1_on();
	int iCounter = 0;
	short connectedToPC = 0;
	char start = 1;
    while ((iCounter <= 100000000 || connectedToPC == 1) && start) 
	{
        if (uart0GetChar(&ch)) 
		{
			connectedToPC = 1;
            switch (ch) 
			{
				case 's':
					start = 0;
                case 'I':
                    grab_frame(1);
                    send_frame();
                    break;
                case 'a':   // 160 x 120
                    camera_reset(160);
                    break;
                case 'b':   // 320 x 240
                    camera_reset(320);
                    break;
                case 'c':   // 640 x 480
                    camera_reset(640);
                    break;
                case 'd':   // 1280 x 1024
                case 'A':   // 1280 x 1024
                    camera_reset(1280);
                    break;
                case 'V':   // send version string
                    serial_out_version();
                    break;
                case 'q':  // change image quality
                    change_image_quality();
                    break;
                case 'X':  // xmodem receive
                    xmodem_receive();
                    break;
                case '$': // prototype zone
                    switch (uart0GetCh()) 
					{
                        case '!':  // reset processor
                            reset_cpu();
                    }
                    break;
                case 't':
                    serial_out_time();
                    break;
				case 'T':
                    set_edge_thresh();
                    break;
                case 'z':  // read, write or dump a flash sector
                    ch = uart0GetCh();
                    if (ch == 'r') {
                      // read user flash sector into flash buffer
                      read_user_flash();
                    }
                    if (ch == 'w') {
                      // write user flash sector from flash buffer
                      write_user_flash();
                    }
                    if (ch == 'Z') {
                      // write boot flash sectors (1-2) from flash buffer
                      write_boot_flash();
                    }
                    if (ch == 'd') {  // dump flash buffer contents to console
                      serial_out_flashbuffer();
                    }
                    if (ch == 'c') {  // clear flash buffer
                      clear_flash_buffer();
                    }
					if (ch == 'C') {  // compute flash buffer checksum
                      crc_flash_buffer();
                    }
                    break;
                case 'g':   // g0 = grab a reference frame and enable differencing
					filterControl(1);
					break;
                case 'G':   // disable frame differencing and color segmentation
                    filterControl(0);
                    break;
				case 'v':  //    v = sample individual pixel, print YUV value
					getColor();
					break;
				case 'w':
					findBlobs();
					break;

            }
            while (uart0GetChar(&ch)) // flush recv buffer
                continue;
        }
        led0_on();
		iCounter++;
    }
	start_robochallenge();
	return 1;
}
